import pandas as pd

df = pd.read_csv('合并RID.csv', encoding='gbk')
Xcols=['TAU_bl', 'PTAU', 'TAU', 'ABETA（β 淀粉样蛋白', 'ABETA_bl（β淀粉样蛋白(Aβ)', 'PTAU_bl', 'APOE4（一种基因']
rowNum = df.shape[0]

def completeMCI(typeDict):
    keyList = typeDict.keys()
    def completeKey(k):
        if not k in keyList:
            typeDict[k] = 0
    completeKey('LMCI')
    completeKey('SMC')
    completeKey('EMCI')

allDict=[]
def calcPercentage(colName, nonnanNum):
    typeDict = {}
    for index, row in df.iterrows():
        if not df.loc[index, colName]:
            diseaseType = df.loc[index, 'TYPE']
            if not diseaseType in typeDict.keys():
                typeDict[diseaseType] = 1
            else:
                typeDict[diseaseType] += 1
    completeMCI(typeDict)
    typeDict['MCI'] = typeDict['LMCI']+typeDict['SMC']+typeDict['EMCI']
    for k,v in typeDict.items():
        typeDict[k] = v/nonnanNum
    typeDict['Feature'] = colName
    allDict.append(typeDict)

for i in Xcols:
    df[i] = df[i].isna()
    naNum = df[i].sum()
    naPercentage = naNum/rowNum
    if naPercentage<0.3:
        print()
        print(i, ':')
        numStr = str(naNum)+'/'+str(rowNum)
        print(numStr, naPercentage)
        calcPercentage(i, rowNum-naNum)

df2 = pd.DataFrame(allDict)
df2.to_csv('非nan值各个病比例.csv')